package com.zhihui.online.utils;

import com.zhihui.online.entity.Answer;
import com.zhihui.online.entity.Teacher;

import java.util.ArrayList;
import java.util.List;

public class AnswerHelper {
    public static List<Answer> buildTree(List<Answer> answerList){
        List<Answer> trees = new ArrayList<>();
        for (Answer answer: answerList) {
            if(answer.getParentId().longValue()==0){
                trees.add(findChildren(answer,answerList));
            }
        }
        return trees;
    }

    private static Answer findChildren(Answer answer, List<Answer> treeNodes) {
        //数据初始化
        answer.setChildren(new ArrayList<Answer>());
        //遍历递归查找
        for (Answer it:treeNodes) {
            //获取当前菜单id
//            String id = sysMenu.getId();
//            long cid = Long.parseLong(id);
            //获取所有菜单parentid
//            Long parentId = it.getParentId();
            //比对
            if(answer.getId()==it.getParentId()) {
                if(answer.getChildren()==null) {
                    answer.setChildren(new ArrayList<>());
                }
                answer.getChildren().add(findChildren(it,treeNodes));
            }
        }
        return answer;
    }
}
